﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinCourseControls.DAL;
using WinCourseControls.Models;

namespace WinCourseControls
{
    public partial class FrmComboBox : Form
    {
        public FrmComboBox()
        {
            InitializeComponent();
        }
        List<GradeInfo> gradeList = null;
        /// <summary>
        /// 下拉列表数据加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmComboBox_Load(object sender, EventArgs e)
        {
            cboList02.Items.AddRange(new object[] { "管理员", "学生", "主任" });//项数不多，固定

            //数据源  绑定---不会动态添加移除、
            GradeDAL gradeDAL = new GradeDAL();
           gradeList = gradeDAL.GetModelList("", "", 0);
            gradeList.Insert(0, new GradeInfo { GradeId = 0, GradeName = "请选择" });
            cboGrades.DisplayMember = "GradeName";  // 显示文本
            cboGrades.ValueMember = "GradeId";  // 实际值
            cboGrades.DataSource = gradeList;//绑定数据源-不能直接通过Items去添加移除项
            cboGrades.SelectedIndex = 0;

            ////gradeList.ForEach(g => cboList03.Items.Add(g));
            //cboList03.SelectedIndex = 0;//设置默认选择项
            //cboList03.DisplayMember = "GradeName";
            //cboList03.ValueMember = "GradeId";
            //cboList03.DataSource = gradeList;//绑定数据源-不能直接通过Items去添加移除项
            ////gradeList.ForEach(g => cboList03.Items.Add(g));  // 当数据源是动态循环添加项-可以通过Items添加和移除
            //cboList03.SelectedIndex = 0;//设置默认选择项
        }

        /// <summary>
        /// 下拉列表数据移除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            // 项的方式移除
            // cboList03.Items.RemoveAt(2);//可以，当数据源是动态循环添加项，绑定的就不可以
            {
                // 数据源的方式移除
                cboList03.DataSource = null;
                gradeList.RemoveAt(2);
                cboList03.DisplayMember = "GradeName";
                cboList03.ValueMember = "GradeId";
                cboList03.DataSource = gradeList;
                //重置后，重新指定绑定字段
            }

        }

        /// <summary>
        /// 下拉列表级联
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            int gradeId = cboGrades.SelectedValue.GetInt();//获取年级编号
            //加载指定年级的班级列
            ClassDAL classDAL = new ClassDAL();
            List<ClassInfo> classList= classDAL.GetModelList("GradeId=" + gradeId + " and IsDeleted=0","","");
            classList.Insert(0, new ClassInfo { ClassId = 0, ClassName = "请选择" });  // 添加默认选项
            cboClasses.DisplayMember = "ClassName";
            cboClasses.ValueMember = "ClassId";
            cboClasses.DataSource = classList;
            cboClasses.SelectedIndex = 0;
        }

        /// <summary>
        /// 提交获取下拉列表选中项值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            int classId = cboClasses.SelectedValue.GetInt();
            string stuName = txtStuName.Text;
            //提交。。。。。
            MessageBox.Show(classId.ToString());
        }
    }
}
